﻿using CodeCity.K8S.Service;
using Google.Protobuf.Collections;
using Grpc.Core;
using Grpc.Net.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeCity.K8S
{
    public class DockerServiceClient
    {
        private readonly GrpcChannel grpcChannel;

        public DockerServiceClient(GrpcChannel grpcChannel)
        {
            this.grpcChannel = grpcChannel;
        }

        public async System.Threading.Tasks.Task RegisterImageAsync(string username)
        {
            var client = new DockerService.DockerServiceClient(grpcChannel);
            await client.RegisterImageAsync(new RegisterImageRequest()
            {
                Username = username
            });
        }
        public async System.Threading.Tasks.Task DeployImageAsync(string username, int port)
        {
            var client = new DockerService.DockerServiceClient(grpcChannel);
            await client.DeployAsync(new DeployRequest()
            {
                Username = username,
                Port = port
            });
        }
        public async System.Threading.Tasks.Task FetchAsync(string repository)
        {
            var client = new DockerService.DockerServiceClient(grpcChannel);
            await client.FetchAsync(new FetchRequest()
            {
                Repository = repository
            });
        }

        public async System.Threading.Tasks.Task BuildAsync(string repository, string imageName)
        {
            var client = new DockerService.DockerServiceClient(grpcChannel);
            await client.BuildAsync(new BuildRequest()
            {
                Repository = repository,
                ImageName = imageName
            });
        }

        public async System.Threading.Tasks.Task<RunResponse> RunAsync(string imageName, string name, IDictionary<string, string> envs)
        {
            var client = new DockerService.DockerServiceClient(grpcChannel);
            var request = new RunRequest()
            {
                ImageName = imageName,
                Name = name
            };
            request.Envs.AddRange(envs.Select(x => new KeyValue()
            {
                Key = x.Key,
                Value = x.Value
            }));
            return await client.RunAsync(request);
        }

        public async System.Threading.Tasks.Task PushAsync(string imageName)
        {
            var client = new DockerService.DockerServiceClient(grpcChannel);
            await client.PushAsync(new PushRequest()
            {
                Image = imageName
            });
        }

        public async System.Threading.Tasks.Task<DeployFromGiteeResponse> DeployFromGiteeAsync(string username, string password, string token)
        {
            var client = new DockerService.DockerServiceClient(grpcChannel);
            var result = await client.DeployFromGiteeAsync(new DeployFromGiteeRequest()
            {
                Username = username,
                Token = token,
                Password = password,
            });
            return result;
        }


    }
}
